package com.matace.keychain.presentation.view;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;

import com.matace.keychain.R;
import com.matace.keychain.data.TermsRepositoryImpl;
import com.matace.keychain.presentation.viewmodel.TermsViewModel;

/**
 * 条款活动
 * 负责显示使用条款和隐私政策，并处理用户同意操作
 */
public class TermsActivity extends AppCompatActivity {
    private TermsViewModel termsViewModel;

    /**
     * 活动创建时调用
     * 初始化视图、ViewModel和事件监听器
     * @param savedInstanceState 保存的实例状态
     */
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_terms);

        // 初始化ViewModel
        termsViewModel = new ViewModelProvider(this, new ViewModelProvider.Factory() {
            @Override
            public <T extends androidx.lifecycle.ViewModel> T create(Class<T> modelClass) {
                if (modelClass.isAssignableFrom(TermsViewModel.class)) {
                    return (T) new TermsViewModel(new TermsRepositoryImpl(TermsActivity.this));
                }
                throw new IllegalArgumentException("Unknown ViewModel class");
            }
        }).get(TermsViewModel.class);

        // 设置条款文本
        TextView configText = findViewById(R.id.termsTextView);
        termsViewModel.getTermsText().observe(this, configText::setText);

        // 设置确认按钮点击事件
        Button configBtn = findViewById(R.id.confirmButton);
        configBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                termsViewModel.agreeTerms();
                Intent intent = new Intent(TermsActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }
}
